//
//  SearchCell.m
//  EducationAdminClient
//
//  Created by KWSD on 16/1/11.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "SearchCell.h"
#import "Masonry.h"
#import "UIColor+CL.h"
@implementation SearchCell

-(instancetype)initIdentifier:(NSString *)identifer{
    self=  [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    if(self){
        UIView *superView = [self createUI];
        [self.contentView addSubview:superView];
        UIView *superview = self.contentView;
        UIEdgeInsets edge = UIEdgeInsetsMake(0, 0, 0, 0);
        [superView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(superview).insets(edge);
        }];
        _contentArr = [[NSMutableArray alloc]init];
        
        [_contentArr addObject:@"包含"];
        [_contentArr addObject:@"不包含"];
        [_contentArr addObject:@"等于"];
        [_contentArr addObject:@"起始字符"];
        [_contentArr addObject:@"终止字符"];
        [_contentArr addObject:@"为空"];

        
        _contentArr1 = [[NSMutableArray alloc]init];
        
        [_contentArr1 addObject:@"大于"];
        [_contentArr1 addObject:@"大于等于"];
        [_contentArr1 addObject:@"等于"];
        [_contentArr1 addObject:@"为空"];
        
        _contentArr2 = [[NSMutableArray alloc]init];
        
        [_contentArr2 addObject:@"小于"];
        [_contentArr2 addObject:@"小于等于"];
        [_contentArr2 addObject:@"等于"];
        [_contentArr2 addObject:@"为空"];


    }
    return self;
}

-(UIView *)createUI{
    UIView *v =[[UIView alloc]init];
    _la =[[UILabel alloc]init];
    [v addSubview:_la];
    [_la mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(v.mas_left).offset(24/2);
        make.centerY.equalTo(v.mas_centerY);
//        make.height.equalTo(@(40));
        make.width.equalTo(@(70));
    }];
    
    //*** 包含按钮
    _clu = [[ContentBtn alloc]init];
    [v addSubview:_clu];
    [_clu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_la.mas_right).offset(10/2);
        make.centerY.equalTo(v.mas_centerY);
        make.width.equalTo(@(50));
        make.height.equalTo(v.mas_height);
    }];
    
    
    //*** 包含按钮
    _clu1 = [[ContentBtn alloc]init];
    [v addSubview:_clu1];
    [_clu1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_la.mas_right).offset(10/2);
        make.centerY.equalTo(v.mas_centerY);
        make.width.equalTo(@(50));
        make.height.equalTo(v.mas_height);
    }];

    //*** 包含按钮中间
    _cluCenter = [[ContentBtn alloc]init];
    [v addSubview:_cluCenter];
    //*** 区间 3
    _textLeft = [[NormalText alloc]init];
    [v addSubview:_textLeft];

    _textRight = [[CustomTextField alloc]init];
    [v addSubview:_textRight];

    [_cluCenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_textLeft.mas_right);
        make.centerY.equalTo(v.mas_centerY);
        make.width.equalTo(@(50));
        make.height.equalTo(v.mas_height);
    }];

    _la.font =[UIFont systemFontOfSize:28/2];
    
    _la.textColor = [UIColor colorWithRed:0xa3/255.0 green:0xa3/255.0 blue:0xa3/255.0 alpha:1];
    _la.numberOfLines = 0;
    
    _text = [[NormalText alloc]init];
    [v addSubview:_text];
    [_text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_clu.mas_right).offset(38/2);
        make.right.equalTo(v.mas_right).offset(-10);
        make.centerY.equalTo(v.mas_centerY);
    }];
    _text.placeholder = @"请输入";
    _text.font =[UIFont systemFontOfSize:32/2];
    
    _text.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    
    [_textLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_clu.mas_right).offset(38/2);
        make.right.equalTo(_cluCenter.mas_left);
        make.centerY.equalTo(v.mas_centerY);
        make.width.equalTo(_textRight.mas_width);
    }];
    _textLeft.placeholder = @"请输入";
    _textLeft.font =[UIFont systemFontOfSize:32/2];
    
    _textLeft.clearButtonMode = UITextFieldViewModeWhileEditing;

    [_textRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_cluCenter.mas_right).offset(38/2);
        make.right.equalTo(v.mas_right).offset(-10);
        make.centerY.equalTo(v.mas_centerY);
        make.width.equalTo(_textLeft.mas_width);
    }];
    _textRight.placeholder = @"请输入";
    _textRight.font =[UIFont systemFontOfSize:32/2];
    
    _textRight.clearButtonMode = UITextFieldViewModeWhileEditing;

    _startDay = [[timeSelectBtn alloc]init];
    [v addSubview:_startDay];
    [_startDay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_clu.mas_right).offset(0);
        make.width.equalTo(@(100));
        make.centerY.equalTo(v.mas_centerY);
        make.height.equalTo(@(30));
    }];
    _startDay.backgroundColor =[UIColor whiteColor];
    _startDay.titleLabel.font =[UIFont systemFontOfSize:14];
    _startDay.layer.cornerRadius = 4;
    _startDay.layer.borderColor =[[UIColor colorWithHex:0x5da6ff]CGColor];
    _startDay.layer.borderWidth = 1;
    [_startDay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _lineImg = [[UIImageView alloc]init];
    [v addSubview:_lineImg];
    [_lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_startDay.mas_right).offset(5);
        make.width.equalTo(@(10));
        make.centerY.equalTo(v.mas_centerY);
        make.height.equalTo(@(1));
    }];
    _lineImg.backgroundColor = [UIColor blackColor];
    
    _endDay = [[timeSelectBtn alloc]init];
    [v addSubview:_endDay];
    [_endDay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lineImg.mas_right).offset(5);
        make.width.equalTo(@(90));
        make.centerY.equalTo(v.mas_centerY);
        make.height.equalTo(@(30));
    }];
    
    _endDay.backgroundColor =[UIColor whiteColor];
    _endDay.titleLabel.font =[UIFont systemFontOfSize:14];
    _endDay.layer.cornerRadius = 4;
    _endDay.layer.borderColor =[[UIColor colorWithHex:0x5da6ff]CGColor];
    _endDay.layer.borderWidth = 1;
     [_endDay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    _startDay.hidden = YES;
    _endDay.hidden = YES;
    _lineImg.hidden = YES;
   
    [_startDay setTitle:@"请选择" forState:UIControlStateNormal];
    [_startDay setTitleColor:[UIColor colorWithRed:0x5d/255.0 green:0xa6/255.0 blue:0xff/255.0 alpha:1] forState:UIControlStateNormal];
    [_startDay setImage:[UIImage imageNamed:@"下拉_"] forState:UIControlStateNormal];
    [_startDay setImage:[UIImage imageNamed:@"下拉_"] forState:UIControlStateNormal];

    [_endDay setTitle:@"请选择" forState:UIControlStateNormal];
    [_endDay setTitleColor:[UIColor colorWithRed:0x5d/255.0 green:0xa6/255.0 blue:0xff/255.0 alpha:1] forState:UIControlStateNormal];
    [_endDay setImage:[UIImage imageNamed:@"下拉_"] forState:UIControlStateNormal];
    [_endDay setImage:[UIImage imageNamed:@"下拉_"] forState:UIControlStateNormal];

    [_startDay addTarget:self action:@selector(startClick:) forControlEvents:UIControlEventTouchUpInside];
    [_endDay   addTarget:self action:@selector(endClick:) forControlEvents:UIControlEventTouchUpInside];

    
    

    [_clu addTarget:self action:@selector(cluClick:) forControlEvents:UIControlEventTouchUpInside];
    [_clu setTitle:@"包含" forState:UIControlStateNormal];
    [_clu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_clu setImage:[UIImage imageNamed:@"下拉_"] forState:UIControlStateNormal];
    [_clu setTitleColor:[UIColor colorWithRed:0x5d/255.0 green:0xa6/255.0 blue:0xff/255.0 alpha:1] forState:UIControlStateNormal];
    _clu.layer.cornerRadius = 4;
    _clu.layer.borderColor =[[UIColor colorWithHex:0x5da6ff]CGColor];
    _clu.layer.borderWidth = 1;

    
    
    [_clu1 addTarget:self action:@selector(cluClick:) forControlEvents:UIControlEventTouchUpInside];
    [_clu1 setTitle:@"大于" forState:UIControlStateNormal];
    [_clu1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_clu1 setImage:[UIImage imageNamed:@"下拉_"] forState:UIControlStateNormal];
    [_clu1 setTitleColor:[UIColor colorWithRed:0x5d/255.0 green:0xa6/255.0 blue:0xff/255.0 alpha:1] forState:UIControlStateNormal];
    _clu1.layer.cornerRadius = 4;
    _clu1.layer.borderColor =[[UIColor colorWithHex:0x5da6ff]CGColor];
    _clu1.layer.borderWidth = 1;

    [_cluCenter addTarget:self action:@selector(cluClick:) forControlEvents:UIControlEventTouchUpInside];
    [_cluCenter setTitle:@"小于" forState:UIControlStateNormal];
    [_cluCenter setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_cluCenter setImage:[UIImage imageNamed:@"下拉_"] forState:UIControlStateNormal];
    [_cluCenter setTitleColor:[UIColor colorWithRed:0x5d/255.0 green:0xa6/255.0 blue:0xff/255.0 alpha:1] forState:UIControlStateNormal];
    _cluCenter.layer.cornerRadius = 4;
    _cluCenter.layer.borderColor =[[UIColor colorWithHex:0x5da6ff]CGColor];
    _cluCenter.layer.borderWidth = 1;

    self.cluCenter.hidden = YES;
    self.textLeft.hidden = YES;
    self.textRight.hidden = YES;
    self.clu1.hidden = YES;
    return v;
}

-(void)updateLayOutForNormal{
    
    [_clu mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(50));
    }];
    
    self.text.hidden = NO;
    self.clu.hidden  = NO;

    //*** 日期
    self.startDay.hidden = YES;
    self.endDay.hidden = YES;
    self.lineImg.hidden = YES;


    //*** 左右
    self.cluCenter.hidden = YES;
    self.textLeft.hidden = YES;
    self.textRight.hidden = YES;
    self.clu1.hidden = YES;

}
-(void)updateLayOutForTime{
[_clu mas_updateConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(@(0));
}];
    
    self.cluCenter.hidden = YES;
    self.textLeft.hidden = YES;
    self.textRight.hidden = YES;
    self.clu1.hidden = YES;
    
    self.text.hidden = YES;
    self.clu.hidden  = NO;

}

-(void)updateLayOutForNum{
    self.cluCenter.hidden = NO;
    self.textLeft.hidden = NO;
    self.textRight.hidden = NO;
    self.text.hidden = YES;
    self.clu.hidden  = YES;
    self.clu1.hidden = NO;
    
}
-(void)setDic:(NSDictionary *)dic{
    _dic = dic;
}
-(void)cluClick:(ContentBtn *)btn{
    
    _currentBtn = btn;
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]init];
    actionSheet.delegate = self;
    if(self.contentChange){
    for (NSString *str in _contentArr) {
        [actionSheet addButtonWithTitle:str];

    }
    }else if(btn == _cluCenter){
        for (NSString *str in _contentArr2) {
            [actionSheet addButtonWithTitle:str];
            
        }
    }
    else{
        for (NSString *str in _contentArr1) {
            [actionSheet addButtonWithTitle:str];
            
        }

    }
    [actionSheet showInView:self.delegateView];
    

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    [_dic setValue:[NSString stringWithFormat:@"%li", (long)buttonIndex] forKey:@"content"];
    
    
    if (_currentBtn == _cluCenter || _currentBtn == _clu1) {
        [_dic setValue:@"1" forKey:@"change"];
        if(_currentBtn == _cluCenter){
        
        [_dic setValue:@"right" forKey:@"right"];
        }

    }
    if(_currentBtn == _clu)[_clu setTitle:[NSString stringWithFormat:@"%@", _contentArr[buttonIndex]]  forState:UIControlStateNormal];
    if(_currentBtn == _clu1)[_clu1 setTitle:[NSString stringWithFormat:@"%@", _contentArr1[buttonIndex]]  forState:UIControlStateNormal];
    if(_currentBtn == _cluCenter)[_cluCenter setTitle:[NSString stringWithFormat:@"%@", _contentArr2[buttonIndex]]  forState:UIControlStateNormal];

    if (_delegateView) {
        [self.delegateView performSelector:@selector(setSearch:) withObject:_dic];
    }

}
-(void)endClick:(UIButton *)btn{
    if (self.delegateView) {
        [self.delegateView performSelector:@selector(timeSelectBtnClick:) withObject:btn];
    }
}

-(void)startClick:(UIButton *)btn{
    if (_delegateView) {
        [self.delegateView performSelector:@selector(timeSelectBtnClick:) withObject:btn];
    }
}
+(void)fieldRole:(NSString *)role{
    
}


@end
